//
//  NewsInformationViewController.m
//  WXXNews
//
//  Created by WangXiaoXiang on 13-6-27.
//  Copyright (c) 2013年 WangXiaoXiang. All rights reserved.
//

#import "NewsInformationViewController.h"

@interface NewsInformationViewController ()

@end

@implementation NewsInformationViewController

- (void)dealloc
{
    [_docid release];
    [_tableView release];
    [_dataArray release];
    [_showImageVC release];
    [_showVideoVC release];
    [_newsInfo release];
    [super dealloc];
}
- (id)initWithDocid:(NSString *)docid
{
    self = [super init];
    if (self) {
        _docid = docid;
        
    }
    return self;
}
//点击评论
-(void)showComment{

    CommentViewController *commentVC = [[CommentViewController alloc]initWithDocid:_docid andWithReplyBoard:_newsInfo.replyBoard];
    [self.navigationController pushViewController:commentVC animated:YES];
}
-(void)makeView{
    _dataArray = [[NSMutableArray alloc]initWithCapacity:0];
    
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height-44) style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
}
#pragma mark - 表的代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }else{
        return [_newsInfo.contentArray count];
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        TitleCell *cell = [[TitleCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"title"];
        cell.titleLabel.text = _newsInfo.title;
        cell.timeLabel.text = _newsInfo.ptime;
        cell.sourcesLabel.text = _newsInfo.source;
        return cell;
    }
    ContentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"con"];
    if(cell == nil){
        cell = [[ContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"con"];
    }
    cell.delegate = self;
    NSDictionary *dic = [_newsInfo.contentArray objectAtIndex:indexPath.row];
    NSString *key = [[dic allKeys] objectAtIndex:0];
   // NSLog(@"key:%@",key);
    //只是一张图片
    if([key isEqualToString:@"url"]){
        cell.contentLabel.hidden = YES;
        cell.mImageView.hidden = NO;
        cell.playerView.hidden = YES;
        [cell setImageWithURL:[dic objectForKey:key]];
    //是视视频
    }else if([key isEqualToString:@"video"]){
        cell.contentLabel.hidden = YES;
        cell.mImageView.hidden = YES;
        cell.playerView.hidden = NO;
        [cell setPlayerInfo:[dic objectForKey:key]];
    //是文本
    }else if([key isEqualToString:@"text"]){
        cell.contentLabel.hidden = NO;
        cell.mImageView.hidden = YES;
        cell.playerView.hidden = YES;
        NSString *str = [dic objectForKey:key];
        CGSize conSize = [str sizeWithFont:[UIFont systemFontOfSize:kCONTENT_FONT_SIZE] constrainedToSize:CGSizeMake(310, 10000) lineBreakMode:NSLineBreakByCharWrapping];
        cell.contentLabel.frame = CGRectMake(5, 5, 310, conSize.height);
        cell.contentLabel.text = str;
    }
    return cell;
}
//contentCell的代理方法
-(void)selectedImage:(UIImage *)image andType:(OpenType)type  otherURL:(NSString *)url{
    if (type == Normal) {
        _showImageVC = [[ShowImageViewController alloc]initWithImage:image];
        _showImageVC.delegate = self;
        [self.view insertSubview:_showImageVC.view atIndex:[self.view.subviews count]];
    }else if(type == Video){
        _showVideoVC = [[ShowVideoViewController alloc]initWithVideoURL:url];
        _showVideoVC.delegate = self;
        [self.view insertSubview:_showVideoVC.view atIndex:[self.view.subviews count]];
    }
}
//showImage的代理方法
-(void)touchesBegan:(ShowImageViewController *)showImageVC{
    [_showImageVC.view removeFromSuperview];
    _showImageVC =nil;
}
//showVideo的代理方法
-(void)showVideoVCDismiss:(ShowVideoViewController *)showVideoVC{

    [_showVideoVC.view removeFromSuperview];
    _showVideoVC = nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 40;
    }else{
        NSDictionary *dic = [_newsInfo.contentArray objectAtIndex:indexPath.row];
        NSString *key = [[dic allKeys] objectAtIndex:0];
//        NSLog(@"key:%@",key);
        if([key isEqualToString:@"url"]){
            return 150;
        }else if([key isEqualToString:@"video"]){
            return 150;
        
        }else if([key isEqualToString:@"text"]){
            NSString *str = [dic objectForKey:key];
            CGSize conSize = [str sizeWithFont:[UIFont systemFontOfSize:kCONTENT_FONT_SIZE] constrainedToSize:CGSizeMake(310, 10000) lineBreakMode:NSLineBreakByCharWrapping];
            return conSize.height+10;
            
        }
    }
    return 40;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NewsInfoOperate *infoOper = [[NewsInfoOperate alloc]initWithDocid:_docid];
    infoOper.delegate = self;
    [infoOper downLoad];
    
    
    [self makeView];
//    [webView loadHTMLString:nil baseURL:[NSURL URLWithString:_url]];
	// Do any additional setup after loading the view.
}
-(void)downLoadFinish:(NewsInfo *)info{
    //设置表上的内容
//    NSLog(@"title:%@",info.title);
    _newsInfo = info;
    //NSLog(@"%@",info.contentArray);
//    _dataArray = [NSMutableArray arrayWithArray:info.contentArray];
    [_tableView reloadData];
    

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:[NSString stringWithFormat:@"%@条评论",info.replyCount] style:UIBarButtonItemStyleBordered target:self action:@selector(showComment)];
    self.navigationItem.rightBarButtonItem = rightItem;
    [rightItem release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
